Skip to content

feat: enable volume mounts for local func run - #3821

Merged
knative-prow[bot] merged 11 commits into
knative:mainfrom
Ankitsinghsisodya:feat/volume-mounts-local-run-2940
Nov 24, 2026
Merged

knative-prow[bot] merged 11 commits into
knative:mainfrom
Ankitsinghsisodya:feat/volume-mounts-local-run-2940

Conversation

@Ankitsinghsisodya

@Ankitsinghsisodya Ankitsinghsisodya commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2940

Note: Re-submission of #3776, accidentally closed when the head repository was deleted.

Summary

  • func run now mounts the volumes declared in func.yaml (pkg/docker/runner.go → newHostConfig()), alongside the port bindings it already set up.
  • volumeMounts and toMount convert each func.yaml volume into a Docker mount.
  • A volume that can't be mapped locally prints a warning to stderr and is skipped; the run continues.

Volume mapping

Volume Local mount
secret read-only bind of .func/run/secrets/<name>, on HostConfig.Binds as src:dst:ro,z; the directory is created (mode 0755) if absent
configMap read-only bind of .func/run/configmaps/<name>, on HostConfig.Binds as src:dst:ro,z; the directory is created (mode 0755) if absent
emptyDir (default or Memory) tmpfs on Mounts, not an anonymous volume; sizeLimit sets the tmpfs size
persistentVolumeClaim named Docker volume on Mounts, keyed by claimName; not a bind under .func/run/pvcs

Secret and configMap names must be DNS-1123 subdomains. . is rejected because filepath.Join would collapse it onto the parent directory. :z is the SELinux relabel; hosts without SELinux ignore it, and a path containing : is bound without it (with a warning). An empty secret or configMap directory gets a hint to add one file per key.

Every bind source is inside .func/, which satisfies the issue's security constraint: no path outside .func is ever mounted.

Notes

Copilot AI review requested due to automatic review settings May 21, 2026 11:18
@knative-prow
knative-prow Bot requested review from dsimansk and jrangelramos May 21, 2026 11:18
@knative-prow knative-prow Bot added size/L 🤖 PR changes 100-499 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 21, 2026
@knative-prow

knative-prow Bot commented May 21, 2026

Copy link
Copy Markdown

Hi @Ankitsinghsisodya. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds Docker runner support for func.yaml volume definitions by translating them into Docker mount specs, along with unit tests covering mapping rules and error/skip behavior.

Changes:

  • Add volume-to-mount translation (toMount) and mount list construction (volumeMounts) during container creation.
  • Add validation to prevent path traversal for Secret/ConfigMap-backed local bind mounts.
  • Introduce unit tests for volume mapping behavior and warning/skip semantics.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
pkg/docker/runner.go Implements volume→mount conversion and wires mounts into container host config.
pkg/docker/runner_volumes_test.go Adds tests for mount mapping, validation failures, and warning/skip behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/docker/runner.go
return nil
}

func toMount(root string, vol fn.Volume) (mount.Mount, error) {
Comment thread pkg/docker/runner.go Outdated
Comment on lines +358 to +364
// safeLocalName rejects names that contain path separators or traversal
// sequences. Secret and ConfigMap names are used as file-path components; an
// adversarial name such as "../../etc" would otherwise escape the .func tree.
func safeLocalName(name string) error {
if strings.ContainsAny(name, "/\\") || strings.Contains(name, "..") {
return fmt.Errorf("name %q must not contain path separators or \"..\"", name)
}
Comment thread pkg/docker/runner.go Outdated
Comment on lines +330 to +336
func volumeMounts(root string, volumes []fn.Volume, out io.Writer) []mount.Mount {
var mounts []mount.Mount
for _, vol := range volumes {
if vol.Path == nil {
fmt.Fprintf(out, "warning: skipping volume %s: missing path\n", vol.String())
continue
}
Comment thread pkg/docker/runner.go Outdated
if err := os.MkdirAll(src, 0700); err != nil {
return mount.Mount{}, fmt.Errorf("cannot create local secret dir %q: %w", src, err)
}
return mount.Mount{Type: mount.TypeBind, Source: src, Target: target}, nil
Comment thread pkg/docker/runner.go Outdated
if err := os.MkdirAll(src, 0750); err != nil {
return mount.Mount{}, fmt.Errorf("cannot create local configmap dir %q: %w", src, err)
}
return mount.Mount{Type: mount.TypeBind, Source: src, Target: target}, nil
want := filepath.Join(root, fn.RunDataDir, "run", "configmaps", "app-config")
if m.Source != want {
t.Errorf("source: got %q, want %q", m.Source, want)
}
@lkingland lkingland added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels May 22, 2026
Comment thread pkg/docker/runner.go

if vol.EmptyDir != nil {
// Both mediums map to tmpfs locally: EmptyDir is ephemeral (pod-lifetime)
// and anonymous Docker volumes persist across runs, leaking storage.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of the body mismatch: this is tmpfs for EmptyDir (including default medium), not an anonymous volume as the PR table says.

Comment thread pkg/docker/runner.go Outdated
}

func newHostConfig(host, port string) (c container.HostConfig, err error) {
func newHostConfig(host, port string, f fn.Function, out io.Writer) (c container.HostConfig, err error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming it out is misleading, as it's realy error out, and there's no comments. At a minimum it should be errOut

@lkingland lkingland left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .func/-rooted secret/configMap binds and skip-and-warn on bad volumes look right. Pointer soup on fn.Volume is the existing YAML model; not something this PR invented.

The review table in the PR body does not match the code. Worth fixing so nobody reviews from the table:

  • emptyDir: {} is described as an anonymous volume; the code mounts tmpfs (same as medium: Memory; the comment about avoiding leaks is the reason).
  • PVC is described as a bind of .func/run/pvcs/<claim>; the code uses a named Docker volume keyed by claimName.

A few Copilot notes I agree with:

  • Secret and ConfigMap mounts in Kubernetes are read-only. Setting ReadOnly: true on those binds would match cluster semantics (and not mask "I wrote the secret file" surprises locally).
  • safeLocalName rejecting any ".." substring is a bit eager (foo..bar is not traversal). Rejecting path separators, empty names, and real .. segments is enough.
  • toMount assuming a non-nil Path is fine as long as only volumeMounts calls it (it already skips nil). A one-line guard would make the helper reusable. Same idea if out can be nil; io.Discard is cheap.

The ConfigMap test not asserting Target is tiny; nice if you are already touching tests.

Once the body matches the code (and ReadOnly if you agree), this looks good to me.

Ankitsinghsisodya and others added 7 commits November 24, 2026 13:21
- Updated newContainer and newHostConfig functions to accept an output writer for logging.
- Introduced volumeMounts and toMount functions to handle Docker volume specifications from function definitions.
- Added support for Secrets, ConfigMaps, EmptyDir, and PVCs in volume mounts, improving flexibility for container configurations.
- Implemented warnings for missing paths and errors during volume mapping, enhancing user feedback during execution.
- Introduced comprehensive unit tests for the volume mount functionality, covering various volume types including Secrets, ConfigMaps, EmptyDir, and PVCs.
- Verified correct behavior for scenarios such as missing paths, read-only mounts, and unrecognized volume types.
- Enhanced error handling and user feedback through warnings for skipped volumes and detailed error messages.
- Ensured that the tests validate the expected mount configurations and behaviors, improving overall test coverage for the Docker runner.
- Adjusted comment formatting in the TestVolumeMounts_SkipsNilPath test case for improved readability.
- Ensured consistency in comment style for better understanding of test scenarios.
- Introduced unit tests to ensure that secret and configMap names containing path traversal sequences are rejected, enhancing security by preventing directory traversal attacks.
- Implemented a new function, safeLocalName, to validate names against path separators and traversal patterns, improving error handling in volume mounting.
- Updated the toMount function to utilize the new validation, ensuring robust handling of volume specifications.
Secret and ConfigMap binds were writable and mode 0700, so a
non-root container user could not read them and a local write
could change the secret. Mount them read-only at 0755.

safeLocalName rejected any ".." substring, so a legal name like
foo..bar failed. Reject separators, empty names, and a real ".."
segment only.

The warning writer is errOut, and a nil writer is io.Discard.
toMount guards a nil path. The ConfigMap test checks Target. An
empty secret or configMap directory tells the user to put files
there. The toMount comment had landed on safeLocalName.
A secret named run made the walk stop at the leaf, so the parent
secrets directory could stay owner-only under a tight umask. Stop
at the .func/run directory itself, and refuse a path outside it
before changing any mode.
The looser check allowed ".", and filepath.Join collapses that to
the parent directory, so one volume would mount every local secret.
Secret and ConfigMap names are DNS-1123 subdomains on the cluster,
which also rejects empty, "..", uppercase, and consecutive dots.

Announce a secret or configMap directory only when this run creates
it. An emptyDir sizeLimit becomes the tmpfs size. mount.Mount cannot
set an SELinux :z relabel; that limit is stated on toMount.
@knative knative deleted a comment from github-actions Bot Nov 24, 2026
The chmod commit picked up generate/zz_filesystem_generated.go
while make test was rewriting it. That file matches eb77e83.
Restore it so the branch does not carry a template embed.

The engine bind-mounts the secret directory itself, so host
parent modes do not matter to the container. chmod only a
directory this run creates, and leave an existing mode alone.
Windows stores chmod 0700 as 0777, so the test failed even when
the existing directory was left alone. Compare the mode the OS
actually recorded.
Secret and ConfigMap mounts move to HostConfig.Binds as
src:dst:ro,z so Docker and Podman relabel on SELinux.
Non-SELinux hosts ignore the flag. emptyDir and PVC stay
on Mounts. Name checks and the create-time hint are unchanged.
The Linux daemon splits a bind on colon. A drive letter or a
colon in the function root would make container create fail.
Those paths stay read-only Mounts and warn. Windows has no
host SELinux, so :z buys nothing there.
@lkingland
lkingland force-pushed the feat/volume-mounts-local-run-2940 branch from a36f800 to 36095f8 Compare November 24, 2026 05:23
@knative-prow knative-prow Bot added size/XL 🤖 PR changes 500-999 lines, ignoring generated files. and removed size/L 🤖 PR changes 100-499 lines, ignoring generated files. labels Nov 24, 2026
@lkingland

Copy link
Copy Markdown
Member

Thanks @Ankitsinghsisodya. We rebased this and finished the review items on top of your commits: read-only secret and configMap binds with the SELinux relabel, DNS-1123 name checks, nil guards, errOut, and the extra test assertions. The description's table now matches the code.

/lgtm
/approve

@knative-prow knative-prow Bot added the lgtm 🤖 PR is ready to be merged. label Nov 24, 2026
@knative-prow

knative-prow Bot commented Nov 24, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Ankitsinghsisodya, lkingland

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added the approved 🤖 PR has been approved by an approver from all required OWNERS files. label Nov 24, 2026
@knative-prow
knative-prow Bot merged commit e3e88e7 into knative:main Nov 24, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved 🤖 PR has been approved by an approver from all required OWNERS files. lgtm 🤖 PR is ready to be merged. ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/XL 🤖 PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Volume Mounts for Local Runs

3 participants